package Action;

import Entities.DoeBookEntity;
import Entities.PurchaseEntity;
import Service.BookDetailService;
import Service.PaymentService;
import Service.PurchaseReceiverService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;

import java.util.List;
import java.util.Map;

public class PurchaseAction extends ActionSupport {
    private PurchaseReceiverService receiverService;
    private PaymentService paymentService;
    private BookDetailService bookDetailService;
    //get from request
    private String bookIdsForOrder;
    float totalPricePerBook = 0;
    private List<DoeBookEntity> doeBookEntityList;

    private String dataJson;
    private String user_name;
    private float payCount;
    private String totalPrice;
    private String result;
    private List<PurchaseEntity> purchaseRecordList;

    public float getTotalPricePerBook() {
        return totalPricePerBook;
    }

    public void setTotalPricePerBook(float totalPricePerBook) {
        this.totalPricePerBook = totalPricePerBook;
    }

    public BookDetailService getBookDetailService() {
        return bookDetailService;
    }

    public void setBookDetailService(BookDetailService bookDetailService) {
        this.bookDetailService = bookDetailService;
    }

    public List<DoeBookEntity> getDoeBookEntityList() {
        return doeBookEntityList;
    }

    public void setDoeBookEntityList(List<DoeBookEntity> doeBookEntityList) {
        this.doeBookEntityList = doeBookEntityList;
    }

    public String getBookIdsForOrder() {
        return bookIdsForOrder;
    }

    public void setBookIdsForOrder(String bookIdsForOrder) {
        this.bookIdsForOrder = bookIdsForOrder;
    }

    public String getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(String totalPrice) {
        this.totalPrice = totalPrice;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public PurchaseReceiverService getReceiverService() {
        return receiverService;
    }

    public void setReceiverService(PurchaseReceiverService receiverService) {
        this.receiverService = receiverService;
    }

    public PaymentService getPaymentService() {
        return paymentService;
    }

    public void setPaymentService(PaymentService paymentService) {
        this.paymentService = paymentService;
    }

    public String getDataJson() {
        return dataJson;
    }

    public void setDataJson(String dataJson) {
        this.dataJson = dataJson;
    }

    public float getPayCount() {
        return payCount;
    }

    public void setPayCount(float payCount) {
        this.payCount = payCount;
    }

    public List<PurchaseEntity> getPurchaseRecordList() {
        return purchaseRecordList;
    }

    public void setPurchaseRecordList(List<PurchaseEntity> purchaseRecordList) {
        this.purchaseRecordList = purchaseRecordList;
    }

    //订单接收处理
    public String execute(){
        user_name = (String) ActionContext.getContext().getSession().get("user_name");
        payCount =receiverService.handle(dataJson);
        return SUCCESS;
    }

    //付款处理器
    public String paymentHandler(){
        user_name = (String) ActionContext.getContext().getSession().get("user_name");
        result = paymentService.paymentSolver(user_name,Float.parseFloat(totalPrice));
        if (result.equals("success"))
        {
            String result2 = paymentService.addPurchaseItem(user_name);
            if(result2.equals("success"))
                return SUCCESS;
            else
                return "fail";
        }
        else
            return "fail";
    }

    //返回用户订单列表
    public String queryPurchase()
    {
        user_name = (String) ActionContext.getContext().getSession().get("user_name");
        purchaseRecordList = paymentService.purchaseItemQuery(user_name);
        return "success";
    }

    public String generateOrder(){
        doeBookEntityList = bookDetailService.getBooksByIds(bookIdsForOrder);
        totalPricePerBook = 0;
        for (DoeBookEntity doeBookEntity : doeBookEntityList){
            totalPricePerBook+=Float.parseFloat(doeBookEntity.getPrice());
        }
        return SUCCESS;
    }

}
